package com.eloo.otsclient.core;

import java.awt.image.BufferedImage;
import java.util.List;

import javax.imageio.ImageReader;
import javax.swing.ImageIcon;

import com.eloo.otsclient.ui.OTSClientUI;
import com.eloo.otsclient.ui.QRDialog;
import com.gargoylesoftware.htmlunit.TopLevelWindow;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebWindow;
import com.gargoylesoftware.htmlunit.html.DomElement;
import com.gargoylesoftware.htmlunit.html.DomNodeList;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlCheckBoxInput;
import com.gargoylesoftware.htmlunit.html.HtmlDivision;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class OTSOrderTicket extends OTSBasicPage{
	private static OTSSignal isRunning = new OTSSignal();
	public  OTSOrderTicket(){ 
	// this.webClient=webClient;
	}
	
	public static void setOTOTSSignal(OTSSignal os){
		isRunning=os;
	}
	
	public synchronized static boolean propareOrder(final WebClient _webClient,HtmlAnchor yda,StringBuffer msg) throws Exception{
		
		isRunning.setIsRunning(false);
		final HtmlPage order = (HtmlPage) yda.click();
		HtmlImage qrcode = (HtmlImage) order.getElementById("img_rrand_code");
		ImageReader imr = qrcode.getImageReader();
		BufferedImage myPicture = imr.read(0);
		QRDialog qd = new QRDialog(OTSClientUI.getJframeMain(),"有票", msg.toString(), new ImageIcon(myPicture)) {
			private static final long serialVersionUID = 1L;
			public void afterOK(String qrString) {
				try {
			 
						
						 OTSClientUI.getJtextareaInfo().append("\n 开始订票！");
//						/OTSOrderTicket qo = new OTSOrderTicket();
						 startOrder(_webClient,order,qrString); 
						 isRunning.setIsRunning(true);
						 
						 synchronized (this) {
							 notifyAll();		
						 }
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			@Override
			public void afterCancel() {
				setContinue(true);
				clearAndHide();
			}
		};
/*
		if (qd.isContinue()) {
		//	System.out.println(order.asText());
			Thread.sleep(6000); 
			continue;
		} else {
			break;
		}
	*/
		return qd.isContinue();
		
	}

	public  static void startOrder(WebClient webClient,HtmlPage order,String qrCode)throws Exception{

		 HtmlDivision showPassengerFilter = order.getHtmlElementById("showPassengerFilter"); 
		 HtmlCheckBoxInput check= (HtmlCheckBoxInput) showPassengerFilter.getFirstElementChild().getFirstElementChild().getFirstChild();
		 Thread.sleep(1000);
		 HtmlPage afterClick= check.click();  
		 logger.info(afterClick.asText());
		 HtmlForm confirmPassenger = afterClick.getFormByName("save_passenger_single");  
		 confirmPassenger.setAttribute("target", "_blank");  
		 HtmlTextInput rand=(HtmlTextInput)confirmPassenger.getElementById("rand");
		 rand.setValueAttribute(qrCode); 
		 HtmlButton sumbit=(HtmlButton) confirmPassenger.getElementsByAttribute("button","tabindex","102").get(0);  
		 sumbit.mouseOver();
		 sumbit.mouseDown();
	 	 HtmlPage submitAction = (HtmlPage) sumbit.click();  
	 	 logger.info(submitAction.asText()); 
	 	 logger.info(((HtmlPage)submitAction.getEnclosingWindow().getEnclosedPage()).asText());
	 	 submitAction.getWebClient().waitForBackgroundJavaScriptStartingBefore(30000);
	     submitAction.getWebClient().waitForBackgroundJavaScript(30000);
	     logger.info(submitAction.asText()); 
	     List<TopLevelWindow> windows= webClient.getTopLevelWindows();
	     if(windows.size()>0){
	     WebWindow latestWindow =  windows.get(0); 
	 	 HtmlPage	confirmPassengerAction = (HtmlPage) latestWindow.getEnclosedPage(); 
	 	 logger.info(confirmPassengerAction.asText());
		  DomNodeList<DomElement>   ofbuton=confirmPassengerAction.getElementsByTagName("button");
	 	  if(ofbuton.size()>0){
			  logger.info("有订票按钮！"+(ofbuton)); 
			  OTSClientUI.getJtextareaInfo().append("\n 有订票按钮！");
		  }
	 	
		  System.out.println("confirmPassengerAction"+ confirmPassengerAction.asText()); 
		  HtmlButton submitbutton;
		  try{
		    submitbutton=(HtmlButton) confirmPassengerAction.getElementsByTagName("button").get(1); 
		  }catch (Exception e) {
			  return;
		  }
		  submitbutton.removeAttribute("disabled");
		  submitbutton.removeAttribute("class");
		  logger.info("submitbutton"+ submitbutton.asText());
		  Thread.sleep(500);
		  HtmlPage	ordersuccess = (HtmlPage)	submitbutton.click(); 
		  logger.info(ordersuccess.asText()); 
		  OTSClientUI.getJtextareaInfo().append("\n 订票结束！");
		  logger.info("订票结束！"); 
	     }
	}
}
